package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import common.EarthSimSettings;

public class QueryDateTimePanel extends JPanel implements KeyListener {
  private static final long serialVersionUID = 151999493513620493L;
  private EarthSimSettings settings = EarthSimSettings.getInstance();
  private JTextField txtStartDate = new JTextField("3/21/2000");
  private JTextField txtStartTime = new JTextField("00:00");
  private JTextField txtEndDate = new JTextField("3/22/2000");
  private JTextField txtEndTime = new JTextField("00:00");
  
  public QueryDateTimePanel() {
    super();
    
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setBorder(BorderFactory.createTitledBorder("Date/Time Settings (GMT +0000)"));
    setAlignmentX(Component.LEFT_ALIGNMENT);
    
    Dimension dim = new Dimension(290,90);
    setPreferredSize(dim);
    setMaximumSize(dim);
    
    add(getStartDateTime());
    add(Box.createVerticalStrut(10));
    add(getEndDateTime());
    
    settings.setStartDate(txtStartDate.getText());
    settings.setStartTime(txtStartTime.getText());
    settings.setEndDate(txtEndDate.getText());
    settings.setEndTime(txtEndTime.getText());
  }
  
  private JPanel getStartDateTime() {
    JPanel pnl = new JPanel();
    pnl.setLayout(new BoxLayout(pnl, BoxLayout.LINE_AXIS));
    pnl.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    txtStartDate.addKeyListener(this);
    txtStartTime.addKeyListener(this);
    
    pnl.add(new JLabel("Start Date: "));
    pnl.add(txtStartDate);
    pnl.add(new JLabel("Start Time: "));
    pnl.add(txtStartTime);
    return pnl;
  }
  
  private JPanel getEndDateTime() {
    JPanel pnl = new JPanel();
    pnl.setLayout(new BoxLayout(pnl, BoxLayout.LINE_AXIS));
    pnl.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    txtEndDate.addKeyListener(this);
    txtEndTime.addKeyListener(this);
    
    pnl.add(new JLabel("End Date: "));
    pnl.add(txtEndDate);
    pnl.add(new JLabel("End Time: "));
    pnl.add(txtEndTime);
    return pnl;
  }
  
  public void keyReleased(KeyEvent e) {
    if(e.getSource().equals(txtStartDate))
      settings.setStartDate(txtStartDate.getText());
    
    else if(e.getSource().equals(txtStartTime))
      settings.setStartTime(txtStartTime.getText());
    
    else if(e.getSource().equals(txtEndDate))
      settings.setEndDate(txtEndDate.getText());
    
    else if(e.getSource().equals(txtEndTime))
      settings.setEndTime(txtEndTime.getText());
    
  }
  
  public void keyPressed(KeyEvent e) {}
  public void keyTyped(KeyEvent e) {}
  
  public void disable() {
    txtEndDate.setEnabled(false);
    txtEndTime.setEnabled(false);
    txtStartDate.setEnabled(false);
    txtStartTime.setEnabled(false);
  }
  
  public void enable() {
    txtEndDate.setEnabled(true);
    txtEndTime.setEnabled(true);
    txtStartDate.setEnabled(true);
    txtStartTime.setEnabled(true);
  }
}
